const { Controller } = require('egg');

class ProcessController extends Controller {


  /**
   * @api {POST} /order/process/insert 新增订单流程
   * @apiName insert
   * @apiGroup price
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiBody  { String } order_id 订单id
   * @apiBody  { Number } status 状态
   * @apiBody  { String } type 类型
   * @apiBody  { String } title 标题
   *
   * @apiHeader { String } Authorization 注册信息
   */

  async insert() {
    const ctx = this.ctx;
    const app = this.app;
    try {
      const param = ctx.request.body;

      const { order_id, status, type, title } = await app.rule.process.insert(param);

      const data = await ctx.service.process.insert(order_id, status, type, title);
      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 200;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }


  /**
   * @api {GET} /order/process/stage 订单阶段
   * @apiName insert
   * @apiGroup price
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiQuery  { String } order_id 订单id
   * @apiQuery  { String } order 排序字段
   * @apiQuery  { String } method 类型
   * @apiQuery  { String } pageIndex 页码
   * @apiQuery  { String } pageSize 页面大小
   *
   * @apiHeader { String } Authorization 注册信息
   */
  async stage() {
    const ctx = this.ctx;
    const app = this.app;
    try {

      const param = ctx.query;

      const { order_id, order, method, pageIndex, pageSize } = await app.rule.process.stage(param);

      const data = await ctx.service.process.stage(order_id, order, method, pageIndex, pageSize);

      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 200;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }

  /**
   * @api {POST} /order/process/update 更新流程
   * @apiName insert
   * @apiGroup price
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiBody  { String } order_id 订单id
   * @apiBody  { Number } status 状态
   * @apiBody  { String } remark 类型
   *
   * @apiHeader { String } Authorization 注册信息
   */

  async update() {
    const ctx = this.ctx;
    const app = this.app;

    try {

      const param = ctx.request.body;

      const { order_id, status, remark } = await app.rule.process.update(param);

      const data = await ctx.service.process.update(order_id, status, remark);

      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 200;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }

  /**
   * @api {POST} /order/process/finish 完成
   * @apiName insert
   * @apiGroup price
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiBody  { String } order_id 订单id
   * @apiBody  { String } remark 类型
   *
   * @apiHeader { String } Authorization 注册信息
   */
  async finish() {
    const ctx = this.ctx;
    const app = this.app;

    try {
      const param = ctx.request.body;

      const { order_id, remark } = await app.rule.process.finish(param);

      const data = await ctx.service.process.trxFinish(order_id, remark);

      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 200;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }

  /**
   * @api {POST} /order/process/refuse 拒绝
   * @apiName insert
   * @apiGroup price
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiBody  { String } order_id 订单id
   * @apiBody  { String } remark 类型
   *
   * @apiHeader { String } Authorization 注册信息
   */
  async refuse() {
    const ctx = this.ctx;
    const app = this.app;
    try {


      const param = ctx.request.body;

      const { order_id, remark } = await app.rule.process.refuse(param);

      const data = await ctx.service.process.refuse(order_id, remark);
      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 200;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }


  /**
   * @api {POST} /order/process/change 修改订单流程
   * @apiName insert
   * @apiGroup price
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiBody  { String } order_id 订单id
   * @apiBody  { String } remark 类型
   *
   * @apiHeader { String } Authorization 注册信息
   */
  async change() {
    const ctx = this.ctx;
    const app = this.app;

    try {

      const param = ctx.request.body;

      const { order_id, remark } = await app.rule.process.change(param);

      const data = await ctx.service.process.trxChange(order_id, remark);

      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 200;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }
}

module.exports = ProcessController;
